package com.kd.juhaowan.ui;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;


import com.hjq.http.listener.OnHttpListener;
import com.hjq.toast.ToastUtils;
import com.kd.juhaowan.base.BoxCacheManager;
import com.kd.juhaowan.http.api.exception.TokenException;
import com.kd.juhaowan.utils.LogUtil;
import com.kd.juhaowan.utils.ResUtils;
import com.kd.juhaowan.utils.StatusBarUtil;
import com.kd.juhaowan.view.CustomLoadingDialog;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseActivity extends AppCompatActivity implements OnHttpListener {
    private Unbinder mUnbinder;
    BoxCacheManager manager;
    CustomLoadingDialog customLoadingDialog ;
    protected ActivityResultLauncher<Intent> baseRegister;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.setDarkMode(this);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            View decorView=getWindow().getDecorView();

            int option=View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;

            decorView.setSystemUiVisibility(option);

            getWindow().setStatusBarColor(Color.TRANSPARENT);


                   }
        baseRegister = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                LogUtil.debugLog( "onActivityResult: _____" + result);
                if (result != null) {

                    if (result.getResultCode() == Activity.RESULT_OK) {
                        reInitDate();

                    }
                }
            }
        });
        setContentView(getViewLayoutResourceId());
        mUnbinder = ButterKnife.bind(this);
        manager = BoxCacheManager.getInstence(this);

    }
    public final int getViewIdentifier(String name) {
        return ResUtils.getViewIdentifier(this, name);
    }
    protected abstract int getViewLayoutResourceId();
    protected abstract void reInitDate();

    protected void showLoadingDialog(){
        if(customLoadingDialog==null) customLoadingDialog= new CustomLoadingDialog(this);
//        customLoadingDialog.setOnShowListener(new DialogInterface.OnShowListener() {
//            @Override
//            public void onShow(DialogInterface dialogInterface) {
//                ToastUtils.show("dialog show"+BaseActivity.this.getClass().getSimpleName());
//            }
//        });
        if(customLoadingDialog!=null&&!customLoadingDialog.isShowing()){
            customLoadingDialog.show();
        }
    }
    protected void cancelLoadingDialog(){
        if(customLoadingDialog!=null){
            try {
                customLoadingDialog.dismiss();
            }catch (Exception e){
                e.printStackTrace();
            }
            customLoadingDialog = null;
        }
    }

    @Override
    protected void onDestroy() {

        if(customLoadingDialog!=null&&customLoadingDialog.isShowing()){
            customLoadingDialog.dismiss();
            customLoadingDialog =null;
        }
        super.onDestroy();
    }

    @Override
    public void onHttpSuccess(Object result) {
//        if (result instanceof HttpData){
//            HttpData data =(HttpData)((HttpData<?>) result).getData();
//            if(data.getData() instanceof LoginUser){
//                LoginUser user = (LoginUser)data.getData();
//
//            }
//        }

        LogUtil.debugLog("base success");
    }



    @Override
    public void onHttpFail(Exception e) {
        cancelLoadingDialog();
        if(e instanceof TokenException){
            if(BaseActivity.this instanceof MainActivity||BaseActivity.this instanceof AuthActivity||BaseActivity.this instanceof PlatFormCoinActivity)return;
            manager.writeAutoLogin(false);
            baseRegister.launch(new Intent(BaseActivity.this,LoginAccountActivity.class));
//            startActivity();

//            finish();
        }else {
            ToastUtils.show(e.getMessage());

            LogUtil.debugLog("base faild"+e.getMessage());

        }

    }
}
